#include "TcpServer.hpp"
#include "Protocol.hpp"
#include <iostream>
#include <memory>
#include <functional>
#include <unordered_map>

void Calculate(yui::Request &req, yui::Reaponse *resp)
{
    int x = req.x_;
    int y = req.y_;
    char op = req.op_;
    std::unordered_map<char, std::function<int()>> hash =
        {
            {'+', [&]()
             { return x + y; }},
            {'-', [&]()
             { return x - y; }},
            {'*', [&]()
             { return x * y; }},
            {'/', [&]()
             {
                 if (y == 0)
                 {
                     resp->code_ = 1;
                     return 0;
                 }
                 return x / y;
             }},
            {'%', [&]()
             {
                 if (y == 0)
                 {
                     resp->code_ = 2;
                     return 0;
                 }
                 return x % y;
             }}};
    if (hash.count(op) == 0)
        resp->code_ = 3;
    else
        resp->result_ = hash[op]();
}
int main()
{
    std::unique_ptr<TcpServer> tsvr(new TcpServer(Calculate));
    tsvr->Init();
    tsvr->Start();
    return 0;
}